const BaseHandler     = require('../base-handler');
const SystemServices  = require('../../../constants').SystemServices;
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;
const utils           = require('../../../utils');

class LoginForgotHandler extends BaseHandler {
    get pattern() {
        return "/login/forgot";
    }

    doGET(request, response) {
        response.render('login-forgot');
    }

    doPOST(request, response) {
        var email = request.body.email;
        if (!email) {
            return response.json({
                success: false,
                error: '请输入要找回密码的邮箱！'
            });
        }
        if (utils.verifyEmailAddress(email)) {
            return response.json({
                success: false,
                error: '请输入合法的邮箱地址！'
            })
        }
        this.Controller.checkUserByEmail(email, (result) => {
            response.json(result);
        });
    }

    setup() {
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = LoginForgotHandler;